/**
 * 
 */
package de.deliancourt.jarvis.implementations;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;

import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TransparencyAttributes;
import javax.media.j3d.TriangleArray;
import javax.vecmath.Point3f;
import javax.vecmath.TexCoord2f;

import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.image.ImageException;
import com.sun.j3d.utils.image.TextureLoader;

import de.deliancourt.jarvis.interfaces.ModelLoader;

/**
 * @author Salacryl
 *
 */
public class Configuration {
	
	private URLClassLoader _dcploader;
	
	private File _directory = new File (".").getAbsoluteFile();
	private final String _DCPURL = "file:///"+_directory+"/dcp.jar";
	private URL _dcpurl ;
	private URL _bgurl ;
	private URL _logomodurl;
	public Configuration() throws IOException{
	
		try {
			 _dcpurl = new URL(_DCPURL);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try{
			File f = new File(_dcpurl.toURI());
			if (!f.exists()){
				throw new IOException("File " + _dcpurl.getPath() + " dos not exist! ");
				
				
			}
		}catch (URISyntaxException e){
			e.printStackTrace();
		}
		
	}
	public void loadDefaultConfiguration(){
		_dcploader = new URLClassLoader(new URL[]{_dcpurl},Thread.currentThread().getContextClassLoader());
		_bgurl=_dcploader.getResource("de/deliancourt/dcp/media/textures/background.jpg");
		_logomodurl = _dcploader.getResource("de/deliancourt/dcp/media/models/Logo_cube.dae");
	}
	
	public Background GetBackground(){
		if (_dcploader==null){
			loadDefaultConfiguration();
		}
		Background bg = new Background();
		try{
			TextureLoader textureLoad = new TextureLoader(_bgurl,null);	
			bg.setImage(textureLoad.getImage());
		}
		catch (ImageException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		return bg;
	}
	
	public Background Gettexture(String s){
		if (_dcploader==null){
			loadDefaultConfiguration();
		}
		Background bg = new Background();
		try{
			TextureLoader textureLoad = new TextureLoader(_dcploader.getResource("de/deliancourt/dcp/media/textures/" + s),null);	
			bg.setImage(textureLoad.getImage());
		}
		catch (ImageException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		return bg;
	}
	
	public Shape3D GetLogo(){
		Shape3D tshape = new Shape3D();
		QuadArray Cube = new QuadArray(24, TriangleArray.COORDINATES | TriangleArray.TEXTURE_COORDINATE_2 );
		InputStream f = _dcploader.getResourceAsStream("de/deliancourt/dcp/media/models/Logo_cube.dae");
		ModelLoader loader = new ColladaLoader(f);
		loader.Load();
		int[] indices = loader.GetIndices();
		Point3f[] c = loader.GetVerticesCoord();
		TexCoord2f[] t = loader.GetTextCood(); 
		for (int i = 0;i<indices.length;i=i+3){
			Cube.setCoordinate(i/3, c[indices[i]]);
		}
		
		for (int i = 2;i<indices.length;i=i+3){
			Cube.setTextureCoordinate(0, i/3, t[indices[i]]);
		}
	   
		
		Appearance a = new Appearance();
		TransparencyAttributes ta = new TransparencyAttributes();
		PolygonAttributes pa = new PolygonAttributes();
		
		   
		   Texture img =new TextureLoader(_dcploader.getResource("de/deliancourt/dcp/media/textures/" + loader.GetTextureName()),null).getTexture();	
		    ImageComponent2D myImage = (ImageComponent2D) img.getImage(0);
	        Texture2D texture = new Texture2D();
	        texture = new Texture2D(Texture2D.BASE_LEVEL, Texture2D.RGBA,2048,2048);
	        texture.setImage(0, myImage);
	        texture.setEnable(true);
	          a.setTexture(texture);
	          ta.setTransparencyMode (TransparencyAttributes.FASTEST);
	          pa.setCullFace(PolygonAttributes.CULL_NONE);
	          ta.setTransparency(1.0f);
	          a.setTransparencyAttributes(ta);
	          a.setPolygonAttributes(pa);
	        
	   
		tshape.setGeometry(Cube);
		tshape.setAppearance(a);
		return tshape;
	}
	
	public URI GetLogoModelURI(){
		URI tmp = null ;
		try {
			tmp =  _logomodurl.toURI();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tmp;
	}
	
}
